//
//  CopyCurrentLocationProvider.m
//
//  Created by Ron Olson on 9/5/10.
//  Copyright 2010 Ron Olson. All rights reserved.
//
//  You are free to use this code any way you want.
//  Hopefully it will help you in whatever you're doing.
//

#import "CopyCurrentLocationProvider.h"


@implementation CopyCurrentLocationProvider

/*
 * This is the "advertised" method via:
 *
 *		<key>NSMessage</key>
 *		<string>getSelectedDirectory</string>
 *
 * in the Info.plist file. The method signature is pretty fixed
 * as-is, only the method name is different.
 */
- (void)getSelectedDirectory:(NSPasteboard *)pboard
					userData:(NSString *)userData
					   error:(NSString **)error
{
	NSString *pboardString;
    NSArray *types;
	
    types = [pboard types];
	
	/*
	 * Here we're checking that we're getting the right thing
	 * from the pasteboard
	 */
    if (![types containsObject:NSFilenamesPboardType]) 
	{		
        *error = NSLocalizedString(@"Uh oh: couldn't get the right object.",
								   @"Pasteboard didn't contain an NSFilenamesPboardType object.");
        return;
    }
	
	/*
	 * Now, can we extract what we need from the object?
	 */
    pboardString = [pboard stringForType:NSFilenamesPboardType];
    if (!pboardString) 
	{
	    *error = NSLocalizedString(@"Whoops: couldn't get file path.",
								   @"Could not extract the XML from the NSFilenamesPboardType object.");
        return;
    }
	
	/*
	 * If we're here, that means we have successfully extracted the text from the
	 * pasteboard. The format is XML in the form of:
	 *		<?xml version="1.0" encoding="UTF-8"?>
	 *		<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
	 *		<plist version="1.0">
	 *		<array>
	 *			<string>/Users/ron/Pictures</string>
	 *		</array>
	 *		</plist>
	 */	
	NSLog(@"Here's what we got from the pasteboard: %@", pboardString);
	
	/*
	 * Now get the actual path from the XML ("/Users/ron/Pictures" using the example above)
	 */
	NSString *easyPath = [self extractDirectoryFromXML:pboardString];
	
	if (easyPath == nil)
	{
		NSLog(@"Hmm...couldn't extract the directory path from the XML!");
		return;
	}
	
	/* 
	 * Now we're going to put the string on the general pasteboard.
	 * We cannot return this back to the caller (the Finder in our case)
	 * because the Finder is not expecting anything to be returned.
	 */
	NSPasteboard *pb = [NSPasteboard generalPasteboard];
	[pb declareTypes:[NSArray arrayWithObject:NSStringPboardType] owner:nil];
	[pb setString:easyPath forType:NSStringPboardType];
    
	return;
	
}

/*
 * This method simply extracts the actual path from the XML document using xpath
 */
- (NSString*)extractDirectoryFromXML:(NSString*)pasteboardXML
{
	NSError *xmlParsingError;
	NSXMLDocument *xmlDoc = [[NSXMLDocument alloc] initWithXMLString:pasteboardXML options:NSXMLDocumentTidyXML error:&xmlParsingError];
	
	NSArray *itemNodes;
	NSError *xPathError;
	itemNodes = [xmlDoc nodesForXPath:@"/plist/array/string" error:&xPathError];
	
	if (!itemNodes)
	{
		NSLog(@"Hmmm...got an error when trying to extract the path: %@", xPathError);
		return nil;
	}
	
	/*
	 * Now return the extracted, standardized path
	 */
	NSLog(@"Path is %@", [[[itemNodes objectAtIndex:0] stringValue] stringByStandardizingPath]);		
	return [[[itemNodes objectAtIndex:0] stringValue] stringByStandardizingPath];
}

@end
